package org.freedom.fastdevpt.gos.accounts.service;

import java.util.List;
import java.util.Map;

import org.freedom.fastdevpt.core.base.BaseBz;
import org.freedom.fastdevpt.core.ex.DelException;
import org.freedom.fastdevpt.core.ex.SaveException;
import org.freedom.fastdevpt.core.ex.UpdateException;
import org.freedom.fastdevpt.gos.accounts.jdbc.AccountMapper;
import org.freedom.fastdevpt.gos.accounts.pojo.Account;
import org.freedom.fastdevpt.gos.accounts.pojo.AccountQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("accountService")
public class AccountService implements BaseBz<Integer, Account, AccountQuery> {
	public static final String CACHE_PIX = "gos_account";
	@Autowired
	private AccountMapper accountMapper;

	@Override
	public Account getByPk(Integer pk) {

		return accountMapper.getByPk(pk);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {  SaveException.class })
	@Override
	public void save(Account t) throws SaveException {
		try {
			accountMapper.save(t);
		} catch (Exception e) {
			throw new SaveException(e.getMessage());
		}
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {  SaveException.class })
	@Override
	public void batchSave(List<Account> list) throws SaveException {
		try {
			accountMapper.batchSave(list);
		} catch (Exception e) {
			throw new SaveException(e.getMessage());
		}
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {  UpdateException.class })
	@Override
	public boolean update(Account t) throws UpdateException {

		int aff = 0;
		try {
			aff = accountMapper.update(t);
		} catch (Exception e) {
			throw new UpdateException(e.getMessage());
		}
		return aff == 1 ? true : false;

	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {  DelException.class })
	@Override
	public boolean batchdelete(Integer[] pk) throws DelException {
		int aff = 0;
		try {
			aff = accountMapper.batchdelete(pk);
		} catch (Exception e) {
			throw new DelException(e.getMessage());
		}
		return aff == pk.length ? true : false;
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {  DelException.class })
	@Override
	public boolean delete(Integer pk) throws DelException {
		int aff = 0;
		try {
			aff = accountMapper.delete(pk);
		} catch (Exception e) {
			throw new DelException(e.getMessage());
		}
		return aff == 1 ? true : false;
	}

	@Override
	public List<Account> getByQuery(AccountQuery q) {

		return accountMapper.getByQuery(q);
	}

	@Override
	public List<Map<String, Object>> getForList(AccountQuery q) {

		return accountMapper.getForList(q);
	}

	@Override
	public Long getCountByQuery(AccountQuery q) {

		return accountMapper.getCountByQuery(q);
	}

}
